import { createRouter, createWebHashHistory } from 'vue-router'
import type { RouteRecordRaw } from 'vue-router'
import { useAuthStore, useUserStore } from '@/stores'

const routes: RouteRecordRaw[] = [
  {
    path: '/',
    name: 'Home',
    component: () => import(/* webpackChunkName: "home" */ '../views/Home.vue')
  },
  {
    path: '/about',
    name: 'About',
    component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
  },
  {
    path: '/introduce',
    name: 'Introduce',
    component: () => import(/* webpackChunkName: "introduce" */ '../views/Introduce.vue')
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import(/* webpackChunkName: "auth" */ '../views/Login.vue')
  },
  {
    path: '/register',
    name: 'Register',
    component: () => import(/* webpackChunkName: "auth" */ '../views/Register.vue')
  },
  {
    path: '/forgot',
    name: 'ForgotPassword',
    component: () => import(/* webpackChunkName: "auth" */ '../views/ForgotPassword.vue')
  },
   {
    path: '/role',
    name: 'Role',
    component: () => import(/* webpackChunkName: "role" */ '../views/Role.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/script',
    name: 'Script',
    component: () => import(/* webpackChunkName: "script" */ '../views/Script.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/voice',
    name: 'Voice',
    component: () => import(/* webpackChunkName: "voice" */ '../views/Voice.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/knowledge-base',
    name: 'KnowledgeBase',
    component: () => import(/* webpackChunkName: "knowledge-base" */ '../views/KnowledgeBase.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/role-chat/:conversationId?',
    name: 'RoleChat',
    component: () => import(/* webpackChunkName: "role-chat" */ '../views/RoleChat.vue'),
    props: true,
    meta: { requiresAuth: true }
  },
  {
    path: '/multi-chat',
    name: 'MultiChat',
    component: () => import(/* webpackChunkName: "multi-chat" */ '../views/MultiChat.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/audiobook',
    name: 'AudioBook',
    component: () => import(/* webpackChunkName: "audiobook" */ '../views/AudioBook.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/tts-example',
    name: 'TTSExample',
    component: () => import(/* webpackChunkName: "tts" */ '../components/Common/TTSPlayButtonExample.vue')
  },
  {
    path: '/admin',
    name: 'Admin',
    component: () => import(/* webpackChunkName: "admin" */ '../views/Admin/Admin.vue'),
    meta: { requiresAdmin: true },
    children: [
      {
        path: '',
        redirect: '/admin/dashboard'
      },
      {
        path: 'dashboard',
        name: 'AdminDashboard',
        component: () => import(/* webpackChunkName: "admin" */ '../views/Admin/Dashboard.vue'),
        meta: { requiresAdmin: true }
      },
      {
        path: 'voice',
        name: 'AdminVoice',
        component: () => import(/* webpackChunkName: "admin" */ '../views/Admin/VoiceManagement.vue'),
        meta: { requiresAdmin: true }
      },
      {
        path: 'knowledge',
        name: 'AdminKnowledge',
        component: () => import(/* webpackChunkName: "admin" */ '../views/Admin/KnowledgeManagement.vue'),
        meta: { requiresAdmin: true }
      },
      {
        path: 'role',
        name: 'AdminRole',
        component: () => import(/* webpackChunkName: "admin" */ '../views/Admin/RoleManagement.vue'),
        meta: { requiresAdmin: true }
      },
      {
        path: 'logs',
        name: 'AdminLogs',
        component: () => import(/* webpackChunkName: "admin" */ '../views/Admin/LogsManagement.vue'),
        meta: { requiresAdmin: true }
      }
    ]
  },
  // 404 兜底路由 - 必须放在最后
  {
    path: '/:pathMatch(.*)*',
    name: 'NotFound',
    component: () => import(/* webpackChunkName: "error" */ '../views/NotFound.vue')
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

// 路由守卫：登录验证和管理员权限校验
router.beforeEach((to, from, next) => {
  const authStore = useAuthStore()
  const userStore = useUserStore()

  // 检查是否需要登录
  if (to.meta.requiresAuth) {
    // 检查是否已登录
    if (!authStore.isLoggedIn || authStore.isTokenExpired) {
      // 未登录，跳转到登录页
      next('/login')
      return
    }
  }

  // 检查是否需要管理员权限
  if (to.meta.requiresAdmin) {
    // 检查是否已登录
    if (!authStore.isLoggedIn || authStore.isTokenExpired) {
      // 未登录，跳转到登录页
      next('/login')
      return
    }

    // 检查是否为管理员
    if (!userStore.isAdmin) {
      // 不是管理员，跳转到首页并提示
      console.warn('访问被拒绝：需要管理员权限')
      next('/')
      return
    }
  }

  next()
})

export default router